import resDotpath from '../../router/resDotpath'
// import axios from './axios.js'
// import api from '../../server/api.js'

const organizationData = []

const roleData = []
export default {
  organizationData,
  roleData,
  getDate(type) { //获取当前时间 2018/01/01,15:38,星期五
    let date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let currentDate = date.getDate()
    let hours = date.getHours()
    let minutes = date.getMinutes()
    let seconds = date.getSeconds()
    if (month < 10) month = '0' + month
    if (currentDate < 10) currentDate = '0' + currentDate
    if (hours < 10) hours = '0' + hours
    if (minutes < 10) minutes = '0' + minutes
    if (seconds < 10) seconds = '0' + seconds
    let days = date.getDay();
    switch (days) {
      case 1:
        days = '星期一';
        break;
      case 2:
        days = '星期二';
        break;
      case 3:
        days = '星期三';
        break;
      case 4:
        days = '星期四';
        break;
      case 5:
        days = '星期五';
        break;
      case 6:
        days = '星期六';
        break;
      case 0:
        days = '星期日';
        break;
    }
    let day1 = year + '-' + month + '-' + currentDate
    let day2 = year + '' + month + '' + currentDate
    let timeDate = [year + '/' + month + '/' + currentDate, hours + ':' + minutes, days, day1,day2]
    let timeDate2 = year + '-' + month
    if (type == 'all') {
      return (timeDate)
    } else if (type == 'yearMonth') {
      return (timeDate2)
    }
  },
  getTime2(date) { //当前24小时
    var date = new Date(date)
    var year = date.getFullYear()
    var Month = date.getMonth()+1
    var day = date.getDate()
    var hour = date.getHours()
    var minutes = date.getMinutes()
    var second = date.getSeconds()
    if (Month<10){
      Month = '0' + Month
    }
    if (day<10){
      day = '0' + day
    }
    if (hour<10){
      hour = '0' + hour
    }
    if (minutes<10){
      minutes = '0' + minutes
    }
    if (second<10){
      second = '0' + second
    }
    return year + '-' + Month + '-' + day + ' ' + hour + ':' + minutes + ':' + second
  },
  setLocalStorage (key,value) { //设置localStorage
    window.localStorage.setItem(key, JSON.stringify(value))
  },
  getLocalStorage (key) { //获取localStorage
    return JSON.parse(window.localStorage.getItem(key))
  },
  removeLocalStorage (key) { //删除localStorage
    window.localStorage.removeItem(key)
  },
  /* 创建动态路由 */
  generateRoutes () {
    let menuList = this.getLocalStorage('menulist')
    let routers = []
    routers.push(
      { path: '/login',name: '登录', component: resolve => require(['@/pages/login/login'],resolve)},
    )
    if (menuList) {
      routers = routers.concat(this.setHomeInfo(menuList))
    }
    return routers
  },
  /* 路由主入口 */
  setHomeInfo (menuList) {
    let children = this.dynamicRouter(menuList)
    return [{path: '/home', component: resolve => require(['@/pages/home/home'],resolve),children: children}]
  },
  dynamicRouter (router) {
    let routerArray = []
    router.forEach(item => {
      // if(item.url) {
        // routerArray.push({path: item.url, name:item.name, component: resDotpath[item.url.substring(1)]})
        item.children.forEach(obj =>{
          if(obj.url) {
            routerArray.push({path: obj.url, component: resDotpath[obj.url.substring(1)]})
          }
        })
      // }
    })
    return routerArray
  },
  // 获取枚举数据
  getEnumberation(apiName,className,that) {
    that.$post(that.$api[apiName]).then(response => {
      if (response.data.succeed) {
        that[className] = response.data.data
      }
    })
  },
  // 数组去重
  unique(arr){
    let newArr=[];
    arr.forEach(el=>{
      const result = newArr.findIndex(ol=>{return el.contractId === ol.id})
      if(result == -1){
        newArr.push({
          id: el.contractId,
          customerName: el.customerName,
          // lastReceiptDate: el.lastReceiptDate,
          contractProductVOList:[]
        })  
      }
    })
    return newArr;
  },
  unique2(arr,type){ //选择组合产品的去重
    const res = new Map();
    return arr.filter((a)=>!res.has(a[type])&& res.set(a[type],1));
  },
  getFileType(name) { //判断文件是否是图片
    if(!name) return false;
    var imgType=["gif", "jpeg", "jpg", "bmp", "png"];
    if(RegExp("\.(" + imgType.join("|") + ")$", "i").test(name.toLowerCase())) {
       return true
    } else {
     return false;
    }
 }
}
